package zuosheng.lianbiao;

/**
 * @author lj
 * @Description: 反转单向链表和双向链表
 * @date 2020-11-15 10:37
 * @QQ 851477108
 */


public class ReverseList {
    //单向链表
    public class Node{
        public int value;
        public Node next;
        public Node(int data){
            this.value = data;
        }
    }

    public Node reverseList(Node head){
        Node pre = null;
        Node next = null;

        while (head!=null){
            next = head.next;
            head.next = pre;
            pre = head;
            head = next;
        }
        return pre;
    }

    //双向链表
    public class DoubleNode{
        public int value;
        public DoubleNode last;
        public DoubleNode next;
        public DoubleNode(int data){
            this.value = data;
        }
    }

    public DoubleNode reverseList(DoubleNode head){
        DoubleNode pre = null;
        DoubleNode next = null;

        while (head!=null){
            next = head.next;
            head.next = pre;
            head.last = next;
            pre = head;
            head = next;
        }
        return pre;

    }
}
